package com.x8.mt.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.x8.mt.common.GlobalMethodAndParams;
import com.x8.mt.common.Log;
import com.x8.mt.entity.CollectJob;
import com.x8.mt.entity.Metadata_relationtable;
import com.x8.mt.service.Metadata_relationtableService;

@CrossOrigin(origins = "*", maxAge = 3600)//解决跨域请求的注解
@Controller
@RequestMapping(value = "/metadata_relationtable")
public class Metadata_relationtableController {

	@Resource
	Metadata_relationtableService metadata_relationtableService;
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2017年11月27日
	 * 作用:插入一条metadata_relationtable记录
	 * 参数：name,descirbe(可选),datasource_connectinfoid(外键，datasource_relationtable的主键)
	 */
	@RequestMapping(value = "/insertMetadata_relationtable",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="Metadata_relationtable",operationDesc="插入一条关系表元数据")
	public JSONObject insertMetadata_relationtable(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();
		
//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!(map.containsKey("name") && map.containsKey("datasource_connectinfoid"))){
			responsejson.put("result", false);
			responsejson.put("count",0);
	        return responsejson;
		}
		
		Metadata_relationtable metadata_relationtable = new Metadata_relationtable();
		metadata_relationtable.setName(map.get("name").toString());
		metadata_relationtable.setDatasource_connectinfoid(Integer.parseInt(map.get("datasource_connectinfoid").toString()));
		if(map.containsKey("describe")){			
			metadata_relationtable.setName(map.get("name").toString());
		}
		
		boolean result = metadata_relationtableService.insertMetadata_relationtable(metadata_relationtable);
		responsejson.put("result",result);
		if(result){
			responsejson.put("count",1);
		}else{
			responsejson.put("count",0);
		}
        return responsejson;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2017年11月27日
	 * 作用:根据id查找Metadata_relationtable
	 * 参数：id
	 */
	@RequestMapping(value = "/getMetadata_relationtableByid",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="Metadata_relationtable",operationDesc="根据id查找表元数据")
	public JSONObject getMetadata_relationtableByid(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();
		
//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!map.containsKey("id")){
			responsejson.put("result", false);
			responsejson.put("count",0);
			return responsejson;
		}

		String idStr = map.get("id").toString();
		int id = 0;
		try {
			id = Integer.parseInt(idStr);
		} catch (Exception e) {
		}

		Metadata_relationtable metadata_relationtable = metadata_relationtableService.getMetadata_relationtableByid(id);
		if(metadata_relationtable == null){
			responsejson.put("result", false);
			responsejson.put("count", 0);
		}else{
			JSONObject data = new JSONObject();
			data.put("id", metadata_relationtable.getId());
			data.put("name", metadata_relationtable.getName());
			data.put("describe", metadata_relationtable.getDesribe());
			data.put("datasource_connectinfoid", metadata_relationtable.getDatasource_connectinfoid());
			data.put("collectjobid", metadata_relationtable.getCollectjobid());
			data.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
					.format(metadata_relationtable.getCreatedate()));
			responsejson.put("result", true);
			responsejson.put("data", data);
			responsejson.put("count", 1);
		}

		return responsejson;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2017年11月27日
	 * 作用:根据id获取Metadata_relationtable的Name和Describe
	 * 参数：id
	 */
	@RequestMapping(value = "/getMetadata_relationtableNameAndDescribeByid",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="Metadata_relationtable",operationDesc="根据id获取表元数据的信息")
	public JSONObject getMetadata_relationtableNameAndDescribeByid(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();
		
//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!map.containsKey("id")){
			responsejson.put("result", false);
			responsejson.put("count",0);
			return responsejson;
		}

		String idStr = map.get("id").toString();
		int id = 0;
		try {
			id = Integer.parseInt(idStr);
		} catch (Exception e) {
		}

		Metadata_relationtable metadata_relationtable = metadata_relationtableService.getMetadata_relationtableByid(id);
		if(metadata_relationtable == null){
			responsejson.put("result", false);
			responsejson.put("count", 0);
		}else{
			JSONObject data = new JSONObject();
			data.put("name", metadata_relationtable.getName());
			data.put("describe", metadata_relationtable.getDesribe());
			responsejson.put("result", true);
			responsejson.put("data", data);
			responsejson.put("count", 1);
		}

		return responsejson;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2017年11月27日
	 * 作用:根据datasource_connectinfoid查找Metadata_relationtable
	 * 参数：datasource_connectinfoid
	 */
	@RequestMapping(value = "/getMetadata_relationtableBydatasourceid",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="Metadata_relationtable",operationDesc="根据datasource_connectinfoid查找表元数据")
	public JSONObject getMetadata_relationtableBydatasourceid(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();
		
//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!map.containsKey("datasource_connectinfoid")){
			responsejson.put("result", false);
			responsejson.put("count",0);
			return responsejson;
		}

		String idStr = map.get("datasource_connectinfoid").toString();
		int datasource_connectinfoid = 0;
		try {
			datasource_connectinfoid = Integer.parseInt(idStr);
		} catch (Exception e) {
		}

		List<Metadata_relationtable> metadata_relationtableBydatasources = metadata_relationtableService.getMetadata_relationtableBydatasourceid(datasource_connectinfoid);
		if(metadata_relationtableBydatasources == null || metadata_relationtableBydatasources.isEmpty()){
			responsejson.put("result", false);
			responsejson.put("count", 0);
		}else{
			JSONArray data = new JSONArray();
			for(Metadata_relationtable metadata_relationtable:metadata_relationtableBydatasources){
				JSONObject node = new JSONObject();
				node.put("id", metadata_relationtable.getId());
				node.put("name", metadata_relationtable.getName());
				node.put("describe", metadata_relationtable.getDesribe());
				node.put("datasource_connectinfoid", metadata_relationtable.getDatasource_connectinfoid());
				node.put("collectjobid", metadata_relationtable.getCollectjobid());
				node.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
						.format(metadata_relationtable.getCreatedate()));
				data.add(node);
			}

			responsejson.put("result", true);
			responsejson.put("data", data);
			responsejson.put("count", metadata_relationtableBydatasources.size());
		}		

		return responsejson;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2017年11月29日
	 * 作用:根据name查找Metadata_relationtable
	 * 参数：name
	 */
	@RequestMapping(value = "/getMetadata_relationtableByName",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="Metadata_relationtable",operationDesc="根据name查找表元数据")
	public JSONObject getMetadata_relationtableByName(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();
		
//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!map.containsKey("name")){
			responsejson.put("result", false);
			responsejson.put("count",0);
			return responsejson;
		}

		String name = map.get("name").toString();

		Metadata_relationtable metadata_relationtable = metadata_relationtableService.getMetadata_relationtableByName(name);
		if(metadata_relationtable == null){
			responsejson.put("result", false);
			responsejson.put("count", 0);
		}else{
			JSONObject data = new JSONObject();
			data.put("id", metadata_relationtable.getId());
			data.put("name", metadata_relationtable.getName());
			data.put("describe", metadata_relationtable.getDesribe());
			data.put("datasource_connectinfoid", metadata_relationtable.getDatasource_connectinfoid());
			data.put("collectjobid", metadata_relationtable.getCollectjobid());
			data.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
					.format(metadata_relationtable.getCreatedate()));

			responsejson.put("result", true);
			responsejson.put("data", data);
			responsejson.put("count", 1);
		}		

		return responsejson;
	}

}

